/**
 * Created by hejx on 2018/6/6.
 */

var closeWinAndFlush = function(){
    layer.closeAll('iframe'); //关闭所有的iframe层
    $('#queryBtn').click();
};

app.controller('supplyChainFrameListController', function($scope,$http) {

    $scope.listData = [];

    /**
     * 构造列表所需的数据结构
     */
    $scope.initChainListData = function(data){
        if(data == null) return;
        for (var i = 0; i < data.length; i++){
            $scope.putItem(data[i]);
        }
    };



    $scope.putItem = function(obj){
        var index = 0;
        var hasFind = false;
        for (var i = 0; i < $scope.listData.length; i++){
            if(obj.flow_name == $scope.listData[i].name){
                index = i;
                hasFind = true;
                break;
            }
        }
        if(hasFind){
            $scope.listData[index].chain[obj.flow_sort] = obj.flow_type_name;
        }else{ // 没有找到
            index = $scope.listData.length == 0 ? 0 : $scope.listData.length;
            $scope.listData[index] = {
                name:obj.flow_name,
                chain:[]
            };
            $scope.listData[index].chain[obj.flow_sort] = obj.flow_type_name;
        }

    };

    /**
     * 绑定事件
     */
    $scope.bindEvent = function(){
        $("#add").click(function () {
            Utils.openInputWin("创建框架流程链",'/admin/supplyChain/inputPage');
        });

        $("#edit").click(function () {
            var selected = $('input[name="flowCheckbox"]:checked');
            if(selected.length != 1){
                layer.msg(COMMON.MSG.EDIT_MSG);
                return false;
            }
            Utils.openInputWin("修改框架流程链",'/admin/supplyChain/inputPage?name='+selected[0].value);
        });
    };

    /**
     * 页面加载完成后执行
     */
    $scope.load = function(){
        console.log("页面加载完成");
        $scope.bindEvent();
        $scope.getData();

    };

    // 获取数据
    $scope.getData = function(){
        $http.get('/admin/supplyChain/findList',{}).success(function(data,status,headers,config){
            console.log("数据请求成功");
            console.log(data);
            if(data.status){
                $scope.initChainListData(data.data);
            }
        }).error(function(data,status,headers,config){
            console.error(data);
        });
    };

});
